package com.asohofrucol.web;

import org.springframework.beans.factory.annotation.Autowired;

import com.asohofrucol.dto.AgrarianResourceRegisterDTO;
import com.asohofrucol.dto.AsohofrucolSessionDTO;
import com.asohofrucol.dto.ForestResourceRegisterDTO;
import com.asohofrucol.dto.LivestockResourceRegisterDTO;

public class ValidationController {
	@Autowired
	private AsohofrucolSessionDTO aSession;
	@Autowired
	private RuatDataController ruatDataController;
	
	private Boolean validAmounts = false;
	
	private String errorAmount;
	
	private String formAmount;
	
	private String totalFarmingAmount;
	
	public Boolean getValidAmounts() {
		// total = pecuarios (cantidad por precio venta) + agrarios ( cantidad * precio venta) +  forestal (cantidad *  prercio venta)
		String totalForm = aSession.getSelectedRuatRegister().getFarmingIncome(); // == total
		formAmount = totalForm;
		Long totalAgrarian = 0L;
		for(AgrarianResourceRegisterDTO agrarianResourceRegister: aSession.getAgrarianResource().getAgrarianResourceRegisters()){
			String cantidad = atLeast0(agrarianResourceRegister.getProductionForTheMarket());
			String valor = atLeast0(agrarianResourceRegister.getSalesPriceAverage());
			Long cant = Long.parseLong(cantidad.replaceAll("[^0-9]", ""), 10);
			Long val = Long.parseLong( valor.replaceAll("[^0-9]", ""), 10);
			totalAgrarian = totalAgrarian + cant*val;			
		}
		Long totalForest = 0L;
		for(ForestResourceRegisterDTO forestResourceRegister:aSession.getForestResource().getForestResourceRegisters()){
			String cantidad =  atLeast0(forestResourceRegister.getProductionForTheMarket());
			String valor =  atLeast0(forestResourceRegister.getAverageSalesPrice());
			Long cant = Long.parseLong(cantidad.replaceAll("[^0-9]", ""), 10);
			Long val = Long.parseLong( valor.replaceAll("[^0-9]", ""), 10);
			totalForest = totalForest + cant*val;				
		}
		
		Long totalLiveStock  = 0L;
		for(LivestockResourceRegisterDTO livestockResourceRegister:aSession.getLivestockResource().getLivestockResourceRegisters()){
			String cantidad =  atLeast0(livestockResourceRegister.getProductionForTheMarket());
			String valor =  atLeast0(livestockResourceRegister.getSalesPriceAverage());
			Long cant = Long.parseLong(cantidad.replaceAll("[^0-9]", ""), 10);
			Long val = Long.parseLong( valor.replaceAll("[^0-9]", ""), 10);
			totalLiveStock = totalLiveStock + cant*val;				
		}	
		Long total = totalAgrarian+totalForest+totalLiveStock;
		totalFarmingAmount = total.toString();
		aSession.getSelectedRuatRegister().setFarmingIncome(totalFarmingAmount);
		
		totalForm = atLeast0(totalForm);
		Long totalFormLong = Long.parseLong(totalForm.replaceAll("[^0-9]", ""), 10);
		//if(totalFormLong.equals(total)){
		//	validAmounts = true;
		//}else{
			validAmounts = false;
		//}
		Long dif = totalFormLong-total;
		errorAmount = dif.toString();
		return validAmounts;
	}
	public void setValidAmounts(Boolean validAmounts) {
		this.validAmounts = validAmounts;
	}

	public String getErrorAmount() {
		return errorAmount;
	}
	public void setErrorAmount(String errorAmount) {
		this.errorAmount = errorAmount;
	}
	
	
	public String getFormAmount() {
		return formAmount;
	}
	public void setFormAmount(String formAmount) {
		this.formAmount = formAmount;
	}
	public String getTotalFarmingAmount() {
		return totalFarmingAmount;
	}
	public void setTotalFarmingAmount(String totalFarmingAmount) {
		this.totalFarmingAmount = totalFarmingAmount;
	}
	private String atLeast0(String value){
		if(value==null) value = "0";
		value = value.trim();
		value="0".concat(value);
		return value;
	}
}
